package com.cx.service.impl;

import com.cx.mapper.BrandMapper;
import com.cx.mapper.PostMapper;
import com.cx.pojo.Brand;
import com.cx.pojo.BrandExample;
import com.cx.pojo.Post;
import com.cx.pojo.PostExample;
import com.cx.service.PostService;
import javafx.geometry.Pos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PostServiceImpl implements PostService {

    @Autowired
    PostMapper postMapper;
    @Autowired
    BrandMapper brandMapper;

    @Override
    public Integer insertPost(Post post) {
        Integer rows = postMapper.insertSelective(post);
        return rows;
    }

    @Override
    public Integer deletePost(Integer postId) {
        Integer rows = postMapper.deleteByPrimaryKey(postId);
        return rows;
    }

    @Override
    public Integer updatePost(Integer postId, Post post) {
        PostExample postExample = new PostExample();
        PostExample.Criteria criteria = postExample.createCriteria();
        criteria.andPostIdEqualTo(postId);
        Integer rows = postMapper.updateByExampleSelective(post,postExample);
        return rows;
    }

    @Override
    public List<Post> selectByUserId(Integer userId) {
        PostExample postExample = new PostExample();
        PostExample.Criteria criteria = postExample.createCriteria();
//        criteria.andUserIdEqualTo(userId);
        List<Post> posts = postMapper.selectByExample(postExample);
        return posts;
    }

    @Override
    public List<Post> selectByCarId(Integer carId) {
        PostExample postExample = new PostExample();
        PostExample.Criteria criteria = postExample.createCriteria();
//        criteria.andCarIdEqualTo(carId);
        List<Post> posts = postMapper.selectByExample(postExample);
        return posts;
    }

    @Override
    public List<Post> selectByPostType(String postType) {
        PostExample postExample = new PostExample();
        PostExample.Criteria criteria = postExample.createCriteria();
        criteria.andPostTypeEqualTo(postType);
        List<Post> posts = postMapper.selectByExample(postExample);
        return posts;
    }

    @Override
    public List<Post> selectByPostId(Integer postId) {
        PostExample postExample = new PostExample();
        PostExample.Criteria criteria = postExample.createCriteria();
        criteria.andPostIdEqualTo(postId);

        List<Post> posts = postMapper.selectByExample(postExample);
        return posts;
    }

    @Override
    public List<Post> selectAll() {
        PostExample postExample = new PostExample();
        PostExample.Criteria criteria = postExample.createCriteria();
        List<Post> posts = postMapper.selectByExample(postExample);
        return posts;
    }

    @Override
    public List<Brand> selectAllBrandName() {
        BrandExample brandExample = new BrandExample();
        BrandExample.Criteria criteria = brandExample.createCriteria();
        List<Brand> brands = brandMapper.selectByExample(brandExample);
        return brands;
    }

    @Override
    public Post selectPostByPostName(String postName) {
        PostExample postExample = new PostExample();
        PostExample.Criteria criteria = postExample.createCriteria();
        criteria.andPostNameEqualTo(postName);
        List<Post> posts = postMapper.selectByExample(postExample);
        return posts.get(0);
    }

    @Override
    public List<Post> selectByUserName(String userName) {
        PostExample postExample = new PostExample();
        PostExample.Criteria criteria = postExample.createCriteria();
        criteria.andUserNameEqualTo(userName);
        List<Post> posts = postMapper.selectByExample(postExample);
        return posts;
    }

    @Override
    public List<Post> selectByPostName(String postName) {
        List<Post> posts = postMapper.selectByPostName(postName);
        return posts;
    }

    @Override
    public List<Post> selectByPostTime(String startTime, String endTime) {
        List<Post> posts = postMapper.selectByPostTime(startTime,endTime);
        return posts;
    }

    @Override
    public Integer selectByStatus() {
        Integer amount = postMapper.selectByStatus();
        return amount;
    }

    @Override
    public List<Post> selectByStatusNum(Integer num) {
        PostExample postExample = new PostExample();
        PostExample.Criteria criteria = postExample.createCriteria();
        criteria.andStatusEqualTo(num);
        List<Post> posts = postMapper.selectByExample(postExample);
        return posts;
    }

    @Override
    public Integer updateStatusToZero(Integer postId) {
        Integer rows = postMapper.updateStatusToZero(postId);
        return rows;
    }

    @Override
    public Integer updateStatusToOne(Integer postId) {
        Integer rows = postMapper.updateStatusToOne(postId);
        return rows;
    }

    @Override
    public List<Post> selectPhoto() {
        List<Post> posts = postMapper.selectPhoto();
        return posts;
    }

    @Override
    public Integer updatePostCelAddOne(Integer postId) {
        Integer rows = postMapper.updatePostCelAddOne(postId);

        return rows;
    }

    @Override
    public Integer updatePostCelDelOne(Integer postId) {
        Integer rows = postMapper.updatePostCelDelOne(postId);

        return rows;
    }
}
